package com.x.ganjiang.gen.runner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.x.ganjiang.gen.dao.CanaryUserIService;
import com.x.ganjiang.gen.data.CanaryUserDO;
import com.x.ganjiang.gen.infra.mapper.CanaryUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Slf4j
public class DemoRunner implements ApplicationRunner {

    @Autowired
    private CanaryUserMapper canaryUserMapper;

    @Autowired
    private CanaryUserIService canaryUserIService;

    @Autowired
    private ConfigurableApplicationContext ctx;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(ApplicationArguments args) throws Exception {
        try {
            doRun();
        } catch (Exception e) {
            log.error("DemoRunner.run error", e);
        } finally {
            int code = SpringApplication.exit(ctx, () -> 0); // 0 表示正常退出
            System.exit(code);
        }
    }

    private void doRun() {
        LambdaQueryWrapper<CanaryUserDO> cuWrapper = new LambdaQueryWrapper<>();
        cuWrapper.eq(CanaryUserDO::getUsrName, "张三");
        cuWrapper.eq(CanaryUserDO::getIsDel, "0");
        List<CanaryUserDO> list = canaryUserIService.list(cuWrapper);
        if (list.isEmpty()) {
            log.info("查询数据为空");
            return;
        }
        list.forEach(System.out::println);
    }

}
